package org.bird.zipkin.controller;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/")
public class HomeController {

//    @Autowired
//    private OkHttpClient client;
    
    @Autowired
    private CloseableHttpClient httpClient;
 
    @GetMapping("/service1")
    public String service() throws Exception {
        Thread.sleep(100);
        HttpGet get = new HttpGet("http://localhost:8082/service2");
        CloseableHttpResponse response = httpClient.execute(get);
        return EntityUtils.toString(response.getEntity(), "utf-8");
    }

//    private  Random random = new Random();

//    @RequestMapping("start")
//    public String start() throws InterruptedException, IOException {
//        int sleep= random.nextInt(100);
//        TimeUnit.MILLISECONDS.sleep(sleep);
//        Request request = new Request.Builder().url("http://localhost:9090/foo").get().build();
//        Response response = client.newCall(request).execute();
//        return " [service1 sleep " + sleep+" ms]" + response.body().toString();
//    }
}